import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import javax.swing.JButton;
import java.awt.ComponentOrientation;
import javax.swing.BoxLayout;
import java.awt.Point;

public class PropPanel extends JFrame {

	private static final String PROP_FILE = "props";
	private static final long serialVersionUID = 1L;
	private static final String DEFAULT_PORT = "0";  //  @jve:decl-index=0:
	private static final String DEFAULT_COMMAND = "v4l2vdSource -d /dev/video0 -r 176x144 -f rgb24 -s pipe.yuv";  //  @jve:decl-index=0:
	private static final String DEFAULT_MODPROBE_COMMAND = "gksudo modprobe v4l2vd";  //  @jve:decl-index=0:
	private static final String DEFAULT_PIPE = "pipe.yuv";
	private JPanel jContentPane = null;  //  @jve:decl-index=0:visual-constraint="10,63"
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JTextField jTextField = null;
	private JTextField jTextField1 = null;
	private JButton jButton = null;
	Properties p = new Properties();  //  @jve:decl-index=0:
	private JLabel jLabel21 = null;
	private JLabel jLabel22 = null;
	private JTextField jTextField2 = null;
	private JLabel jLabel23 = null;
	private JTextField jTextField3 = null;
	/**
	 * This is the default constructor
	 */
	public PropPanel() {
		super();
		initialize();
		loadProps();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setContentPane(getJContentPane());
		this.setTitle("Properties");
		this.setLocation(new Point(500, 100));
		this.setSize(new Dimension(430, 228));
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel23 = new JLabel();
			jLabel23.setText("pipe:");
			jLabel22 = new JLabel();
			jLabel22.setText("modprobe command:");
			jLabel21 = new JLabel();
			jLabel21.setText("You need to restart CamServer for changes to take effect!");
			jLabel1 = new JLabel();
			jLabel1.setText("v4l feed command:");
			jLabel = new JLabel();
			jLabel.setText("port:");
			jContentPane = new JPanel();
			jContentPane.setLayout(new BoxLayout(getJContentPane(), BoxLayout.Y_AXIS));
			jContentPane.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
			jContentPane.setSize(new Dimension(435, 130));
			jContentPane.add(jLabel, null);
			jContentPane.add(getJTextField(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getJTextField1(), null);
			jContentPane.add(jLabel22, null);
			jContentPane.add(getJTextField2(), null);
			jContentPane.add(jLabel23, null);
			jContentPane.add(getJTextField3(), null);
			jContentPane.add(jLabel21, null);
			jContentPane.add(getJButton(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setPreferredSize(new Dimension(4, 19));
			jTextField.setText("");
			jTextField.setHorizontalAlignment(JTextField.LEFT);
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setText("Save");
			jButton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent event){
					saveProps();
				}
			});
		}
		return jButton;
	}

	protected void loadProps() {
		try {
			FileReader fr = new FileReader(PROP_FILE);
			p.load(fr);
			fr.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		jTextField.setText(p.getProperty("port", DEFAULT_PORT));
		jTextField1.setText(p.getProperty("command", DEFAULT_COMMAND));
		jTextField2.setText(p.getProperty("modprobe", DEFAULT_MODPROBE_COMMAND));
		jTextField3.setText(p.getProperty("pipe", DEFAULT_PIPE));
	}
	
	protected void saveProps() {
		FileWriter fw = null;
		try {
			fw = new FileWriter(PROP_FILE);
		} catch (IOException e) {
			e.printStackTrace();
		}
		p.setProperty("port",jTextField.getText());
		p.setProperty("command",jTextField1.getText());
		p.setProperty("modprobe",jTextField2.getText());
		p.setProperty("pipe",jTextField3.getText());
		try {
			p.store(fw, "saved");
		} catch (IOException e) {
			e.printStackTrace();
		}
		this.setVisible(false);
	}

	public int getPort(){
		return Integer.parseInt(p.getProperty("port", DEFAULT_PORT));
	}
	
	public String getCommand(){
		return p.getProperty("command", DEFAULT_COMMAND);
	}
	
	public String getModprobeCommand(){
		return p.getProperty("modprobe", DEFAULT_MODPROBE_COMMAND);
	}
	
	public String getPipe(){
		return p.getProperty("pipe", DEFAULT_PIPE);
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
			jTextField1.setPreferredSize(new Dimension(4, 19));
			jTextField1.setHorizontalAlignment(JTextField.LEFT);
		}
		return jTextField1;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
		}
		return jTextField2;
	}

	/**
	 * This method initializes jTextField3	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField3() {
		if (jTextField3 == null) {
			jTextField3 = new JTextField();
		}
		return jTextField3;
	}
	

}  //  @jve:decl-index=0:visual-constraint="64,20"
